#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
THN HTTP Stack Library
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 23-Mar-11 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
using Thn.Collections;
#endregion

namespace Thn.Net.Http
{
    /// <summary>
    /// Helper class for translating string names to strongly-typed name of <see cref="HttpRequestMethod"/>.
    /// </summary>
#if DEBUG || TESTING
    public
#else
    internal
#endif
 class HttpRequestMethodFromText
    {
        #region Fields
        StringDictionary<HttpRequestMethod> mItems;
        #endregion

        #region Get
        /// <summary>
        /// Gets strongly-typed <see cref="HttpRequestMethod"/> from string value.
        /// </summary>
        public static HttpRequestMethod Get(string name)
        {
            name = name.ToUpperInvariant();
            HttpRequestMethod result = HttpRequestMethod.Unknown;
            if (Instance.mItems.ContainsKey(name))
            {
                result = Instance.mItems[name];
            }
            #region Defensive tracing
            else
            {
                //not found
            }
            #endregion

            return result;
        }
        #endregion

        #region Instance
        /// <summary>
        /// Gets an instance of <see cref="type"/>
        /// </summary>
        static HttpRequestMethodFromText Instance
        {
            get
            {
                /// An instance of Singleton wont be created until the very first 
                /// call to the sealed class. This a CLR optimization that ensure that
                /// we have properly lazy-loading singleton. 
                return HttpRequestMethodNamesCreator.CreatorInstance;
            }
        }

        /// <summary>
        /// Sealed class to avoid any heritage from this helper class
        /// </summary>
        private sealed class HttpRequestMethodNamesCreator
        {
            // Retrieve a single instance of a Singleton
            private static readonly HttpRequestMethodFromText mInstance = new HttpRequestMethodFromText();

            /// <summary>
            /// Return an instance of the class <see cref="HttpRequestMethodFromText"/>
            /// </summary>
            public static HttpRequestMethodFromText CreatorInstance
            {
                get { return mInstance; }
            }
        }
        #endregion

        #region Build Cache
        void BuildCache()
        {
            mItems = new StringDictionary<HttpRequestMethod>();

            //HaiNM - 2011 APr 4 - SILVERLIGHT 4.0 does not support Type.GetEnumNames()
            //foreach (var item in typeof(HttpRequestMethod).GetEnumNames())
            //{
            //    mItems.Add(item.ToUpper(), item.Parse<HttpRequestMethod>());
            //}

            foreach (var item in EnumHelper.GetNames(typeof(HttpRequestMethod)))
            {
                mItems.Add(item.ToUpperInvariant(), item.Parse<HttpRequestMethod>());
            }
        }
        #endregion

        #region Private constructor
        /// <summary>
        /// Private constructor to avoid other instantiation
        /// This must be present otherwise the compiler provide 
        /// a default public constructor
        /// </summary>
        private HttpRequestMethodFromText()
        {
            BuildCache();
        }
        #endregion
    }
}
